
//
//  Statuses.swift
//  SinaWeibo
//
//  Created by apple on 15/11/14.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class Statues: NSObject {
    
    //微博创建时间
    var created_at : String?
    
    //微博ID
    var id: Int = 0
    
    //	微博信息内容
    var text : String?
    
    //微博来源
    var source :String?
    
    //用户属性
    var user : User?
    
    //转发微博属性
    var retweeted_status:Statues?
    
    //定义配图视图的属性
    var pic_urls : [[String:String]]?
    
    
    //定义计算性属性 用于单张图片的缓存
    var pictureURLs :[NSURL]? {
        
        if let pictureURLs = retweeted_status?.imageURLs {
            
            //转发微博
            return pictureURLs
        }
        
        //原创微博
        return imageURLs
    }
    
    //计算性属性 
    var imageURLs : [NSURL]? {
        
        guard let urls = pic_urls else {
            
            return nil
        }
        
        var tempArray  = [NSURL]()
        for item in urls {
            
            let url = NSURL(string: item["thumbnail_pic"]!)
            
            tempArray.append(url!)
        }
        
        return tempArray
    }
    
    //KVC赋值
    init(dict:[String:AnyObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dict)
        
    }
    
    //对属性进行赋值
    override func setValue(value: AnyObject?, forKey key: String) {
        
        if key == "user" {
            
            if let dict = value as? [String:AnyObject] {
                
               user = User(dict: dict)
                
            }
            
            return
        }
        if key == "retweeted_status" {
            
            if let dict = value as? [String:AnyObject] {
                
                retweeted_status = Statues(dict: dict)
            }
            return
        }
        
        //对其他的属性进行初始化
        super.setValue(value, forKey: key)
    }
    
    //过滤掉没有写的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
}

